import os.path

import numpy as np
import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="extract min and max values from a list of files")

    parser.add_argument('-i', dest='file', required=True, default="", help='name for model files')
    parser.add_argument('-o', dest='out_file', required=True, default="", help='name for output files')
    parser.add_argument('-dtype', dest='data_type', required=False, default="float64", help='data type of input files')


    options = parser.parse_args()

    list_file = options.file

    if options.data_type == "float64" or options.data_type == "double":
        dtype = np.float64
    elif options.data_type == "float" or options.data_type == "float32":
        dtype = np.float32
    elif options.data_type == "float16" or options.data_type == "half":
        dtype = np.float16
    elif options.data_type == "int16" or options.data_type == "short":
        dtype = np.int16
    elif options.data_type == "int" or options.data_type == "int32":
        dtype = np.int32


    with open(list_file, "r") as fp:
        file_list = fp.readlines()

    fout = open(options.out_file, "w", encoding="utf-8")

    for file in file_list:
        in_file = file.strip('\n')
        data = np.fromfile(in_file, dtype=dtype)
        in_file_true_name = os.path.split(in_file)[-1]
        mini = np.min(data)
        maxi = np.max(data)
        fout.write(in_file_true_name + '\t' + str(mini) + '\t' + str(maxi) + '\n')

    fout.close()
